// 云函数入口文件
const cloud = require('wx-server-sdk');
const _ = require('lodash');
const random = require('random');

cloud.init();
const db = cloud.database();

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  const res = await db.collection('lottery_award').get();
  const awardList = res.data || [];
  // 查询中奖记录
  const record_res = await db.collection('lottery_record').where({ openid: wxContext.OPENID }).limit(1).get();
  const lottery_record = record_res.data[0] || {};
  const result = { awardList, lotteryRecord: null };
  const keys = [ 'award_name', 'award_code', 'lottery_code', 'created_time', 'name', 'telephone', 'room_no' ];
  if (!_.isEmpty(lottery_record)) result.lotteryRecord = _.pick(lottery_record, keys);
  return result;
}